package com.biaoyoo.www.personapp.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * <pre>
 *     author : 40575
 *     e-mail : huyongxi2015@gmail.com
 *     time   : 2017/05/24
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDividerDrawable;
    private int mOrientation = LinearLayoutManager.VERTICAL;

    public DividerItemDecoration(Context applicationContext, int listDivider) {
        mDividerDrawable = applicationContext.getResources().getDrawable(listDivider);
    }
    public DividerItemDecoration(Context applicationContext, int listDivider, int orientation) {
        this.mOrientation = orientation;
        mDividerDrawable = applicationContext.getResources().getDrawable(listDivider);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if(mDividerDrawable == null){
            return;
        }
        if(parent.getChildLayoutPosition(view)<1){
            return; //如果是第一个item，不需要divider
        }
        //相当于给itemView设置margin,
        if(mOrientation == LinearLayoutManager.VERTICAL){
            outRect.top = mDividerDrawable.getIntrinsicHeight();
        }else if(mOrientation == LinearLayoutManager.HORIZONTAL){
            outRect.left = mDividerDrawable.getIntrinsicWidth();
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if(mDividerDrawable == null){
            return;
        }
        int childCount = parent.getChildCount();
        if(mOrientation == LinearLayoutManager.VERTICAL){
            int right = parent.getWidth();
            for(int i=0; i<childCount; i++){
                View child = parent.getChildAt(i);

                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                int left = parent.getPaddingLeft() + child.getPaddingLeft();
                int bottom = child.getTop() - params.topMargin;
                int top = bottom - mDividerDrawable.getIntrinsicHeight();
                mDividerDrawable.setBounds(left, top, right, bottom);
                mDividerDrawable.draw(c);
            }

        }else if(mOrientation == LinearLayoutManager.HORIZONTAL){
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);

                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                int top = parent.getPaddingTop() + child.getPaddingTop();
                int bottom = child.getHeight() + parent.getPaddingTop();
                int right = child.getLeft() - params.leftMargin;
                int left = right - mDividerDrawable.getIntrinsicWidth();
                mDividerDrawable.setBounds(left, top, right, bottom);
                mDividerDrawable.draw(c);
            }
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }
}
